package logic

import (
	"context"
	"fmt"

	"lottery-zero/internal/model"
	"lottery-zero/internal/svc"
	"lottery-zero/internal/types"

	"github.com/tal-tech/go-zero/core/logx"
	"golang.org/x/crypto/bcrypt"
)

type RegisterLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) RegisterLogic {
	return RegisterLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *RegisterLogic) Register(req types.RegisterReq) (*types.CodeReply, error) {

	hash, err := bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost)
	if err != nil {
		fmt.Println("加盐失败", err)
		return nil, err
	}

	ui := model.UserInfo{Email: req.Email, UserAuth: model.UserAuth{Name: req.Username, Passwrod: string(hash)}}
	result := l.svcCtx.PGDB.Create(&ui)

	return &types.CodeReply{Code: 200, Msg: "success"}, result.Error
}
